﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WindowsLive.Writer.Api;

namespace InsertIMDBInfo
{
    public partial class ContentEditor : SmartContentEditor
    {
        ISmartContent m_content;
        PluginSettings m_settings;
        PluginSettings m_default;

        public ContentEditor(PluginSettings defaulttemplate)
        {
            m_default = defaulttemplate;
            InitializeComponent();
            this.SelectedContentChanged += new EventHandler(ContentEditor_SelectedContentChanged);
        }

        void ContentEditor_SelectedContentChanged(object sender, EventArgs e)
        {
            m_content = this.SelectedContent;
            m_settings = new PluginSettings(m_content.Properties);

            ApplySettings();
        }

        private void ApplySettings()
        {
            //chbxActors.Checked = m_settings.DisplayActors;
            //chbxDescription.Checked = m_settings.DisplayDescription;
            //chbxImage.Checked = m_settings.DisplayImage;
            //chbxTitle.Checked = m_settings.DisplayTitle;
            //chbxYear.Checked = m_settings.DisplayYear;            
            cbAlignment.SelectedIndex = m_settings.Alignment;
            SetTemplate();
        }

        private void SetTemplate()
        {
            //Tiny
            //Medium
            //Full
            //Custom
            bool[] options = m_settings.ParseTemplateOptions(m_settings.CustomTemplate);
            if (m_settings.CustomTemplate.Equals(m_settings.FullTemplate))
            {
                cbTemplates.SelectedIndex = 2;
                cbTemplates_SelectedIndexChanged(this, null);
            }
            else if (m_settings.CustomTemplate.Equals(m_settings.TinyTemplate))
            {
                cbTemplates.SelectedIndex = 0;
                cbTemplates_SelectedIndexChanged(this, null);
            }
            else if (m_settings.CustomTemplate.Equals(m_settings.MediumTemplate))
            {
                cbTemplates.SelectedIndex = 1;
                cbTemplates_SelectedIndexChanged(this, null);
            }
            else
            {
                cbTemplates.SelectedIndex = 3;
                cbTemplates_SelectedIndexChanged(this, null);
            }
            ApplyTemplate(options);
        }

        private void ApplyTemplate(bool[] options)
        {
            chbxActors.Checked = options[0];
            chbxDescription.Checked = options[1];
            chbxImage.Checked = options[2];
            chbxTitle.Checked = options[3];
            chbxYear.Checked = options[4];
        }

        private void lnklblImdbLink_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process process = new System.Diagnostics.Process();
            process.StartInfo.UseShellExecute = true;
            process.StartInfo.FileName = m_settings.IMDbLink;
            process.Start();
        }

        private void chbxTitle_CheckedChanged(object sender, EventArgs e)
        {
            SetCustomTemplate();
            OnContentEdited();
        }

        private void chbxActors_CheckedChanged(object sender, EventArgs e)
        {
            m_settings.CustomTemplate = SetCustomTemplate();
            OnContentEdited();
        }

        private void chbxImage_CheckedChanged(object sender, EventArgs e)
        {
            m_settings.CustomTemplate = SetCustomTemplate();
            OnContentEdited();
        }

        private void chbxDescription_CheckedChanged(object sender, EventArgs e)
        {
            m_settings.CustomTemplate = SetCustomTemplate();
            OnContentEdited();
        }

        private void chbxYear_CheckedChanged(object sender, EventArgs e)
        {
            m_settings.CustomTemplate = SetCustomTemplate();
            OnContentEdited();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            m_settings.Alignment = cbAlignment.SelectedIndex;
            OnContentEdited();
        }

        private void cbTemplates_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (cbTemplates.SelectedIndex)
            {
                case 0:
                    grpDisplay.Visible = false;
                    m_settings.CustomTemplate = m_settings.TinyTemplate;
                    break;
                case 1:
                    grpDisplay.Visible = false;
                    m_settings.CustomTemplate = m_settings.MediumTemplate;
                    break;
                case 2:
                    grpDisplay.Visible = false;
                    m_settings.CustomTemplate = m_settings.FullTemplate;
                    break;
                case 3:
                    grpDisplay.Visible = true;
                    m_settings.CustomTemplate = SetCustomTemplate();
                    break;               
            }
            OnContentEdited();
        }

        private string SetCustomTemplate()
        {
            string displayoptions = string.Format("{0},{1},{2},{3},{4}",
                                                      chbxActors.Checked.ToString(),
                                                      chbxDescription.Checked.ToString(),
                                                      chbxImage.Checked.ToString(),
                                                      chbxTitle.Checked.ToString(),
                                                      chbxYear.Checked.ToString());
            return displayoptions;
        }

        private void lnkDefault_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            m_default.DefaultTemplate = SetCustomTemplate();
        }
    }
}
